<template>
	<view class="pay">
		<!-- 比较适合进入到APP.vue页面是请求获取默认地址 同事设置到状态管理器中 -->
		<van-contact-card type="edit" :tel="address.tel" :name="address.name" @click="goDefaultContact" />

		<van-card v-for="(cg,cg_idx) in cartGoods" :num="cg.num" :price="cg.price/100" :origin-price="cg.pre_price"
			:desc="cg.desc" :title="cg.name" :thumb="cg.goods_thumb">
		</van-card>
		<!-- 选择是否使用优惠券 -->

		<!-- 有积分 考虑是否使用积分 -->

		<!-- 重新计算商品总价值 确认最终支付 -->
		<view class="center flex">
			<view class=""></view>
			<view class="total">￥{{total/100}}</view>
		</view>
		<view class="center btns">
			<van-button @click="WXPay" size="large" type="success">微信支付</van-button>
			<van-button @click="QBPay" size="large" type="warning">余额支付</van-button>
		</view>
	</view>
</template>

<script>
	const UserDB = uniCloud.importObject('user');
	const GoodsDb = uniCloud.importObject('goods-model');
	const OrderDb = uniCloud.importObject('order');
	import Storage from '@/utils/storage.js'
	import Msg from '@/utils/msg.js'
	export default {
		data() {
			return {
				cartGoods: [],
				total: 0
			}
		},
		computed: {
			address() {
				if (!this.$store.state.address.address.name) {
					this.$store.commit("storageToAddress")
				}
				return this.$store.state.address.address
			}
		},
		onLoad() {
			let userInfo = Storage.UserInfo
			this.getAddress(userInfo._id)
			this.getPayGoods()
		},
		methods: {
			getPayGoods() {
				let list = Storage.get('countCart')
				this.cartGoods = list;
				GoodsDb.payGoods(list).then(res => {
					// 删除库存不足的商品
					// res.data.map((val, k) => {
					// 	if (!val) {
					// 		this.cartGoods.splice(k, 1)
					// 	}
					// })
				})
				this.countCart(this.cartGoods)
			},
			countCart(cartData) {
				let total = 0;
				for (let i = 0; i < cartData.length; i++) {
					const elem = cartData[i];
					total = total + (elem.price * elem.num)
				}
				// 计算完总价以后 考虑优惠卷 减钱
				this.total = total;
			},
			getAddress(userId) {
				UserDB.defaultAddress(userId).then(res => {
					let ad = res.data[0]
					this.address_id = ad._id;
					this.tel = ad.mobile;
					this.name = ad.name
				})
			},
			goDefaultContact() {
				uni.navigateTo({
					url: "/pages/user/address/list"
				})
			},
			WXPay() {
				// 调起微信支付
				// 支付完后无论是否成功进入订单管理页面
				uni.redirectTo({
					url: "/pages/order/order"
				})
			},
			QBPay() {
				let that = this;
				UserDB.money(Storage.UserInfo._id).then(res => {
					let money = res.data.money
					uni.showModal({
						title: '支付消息',
						content: `你的当前余额是${money/100}元,是否支付？`,
						success(res) {
							if (res.confirm) {
								if (money - that.total > 0) {
									OrderDb.QBPay(
										that.cartGoods,
										Storage.UserInfo._id,
										that.total,
										that.$store.state.address.address
									).then(res => {
										
										uni.redirectTo({
											url:"/pages/order/order"
										})
									}).catch(e => {
										console.log(e);
									})
								} else {
									Msg.error("账户余额不足")
								}
							}
						}
					})
				})
				// uni.redirectTo({
				// 	url: "/pages/order/order"
				// })
			}

		}
	};
</script>

<style>
	.center.btns {
		height: 260rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.total {
		color: red;
		font-size: 50rpx;
		font-weight: bold;
	}
</style>
